/**
 * <p>
 * Created on 2006-6-12 19:31:09 <br>
 * Copyright (c) 2006, All rights reserved.
 * </p>
 * @author Widen
 */
package com.widen.prima.editor.finance;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.widen.prima.Messages;
import com.widen.prima.finance.entites.AccountSubjectBo;
import com.widen.prima.finance.entites.BookOfAccountBo;
import com.widen.prima.finance.entites.Direction;
import com.widen.prima.util.Util;

public class BookOfAccountInputCellModifier
    implements ICellModifier
{
    private Viewer viewer;
    private Shell shell = Display.getCurrent().getActiveShell();

    public BookOfAccountInputCellModifier(Viewer viewer)
    {
        this.viewer = viewer;
    }

    /**
     * Returns whether the property can be modified
     * 
     * @param element the element
     * @param property the property
     * @return boolean
     */
    public boolean canModify(Object element, String property)
    {
        // Allow all to be modified.
        return true;
    }

    /**
     * Returns the value for the property
     * 
     * @param element the element
     * @param property the property
     * @return Object
     */
    public Object getValue(Object element, String property)
    {
        BookOfAccountBo boaBo = (BookOfAccountBo) element;
        if (BookOfAccountInputPanel.SUBJECT_NAME.equals(property))
        {
            AccountSubjectBo subjectBo = boaBo.getAccountSubject();
            return subjectBo;
        } else if (BookOfAccountInputPanel.DIRECTION.equals(property))
        {
            if (boaBo.getDirection() == null)
            {
                return new Integer(0);
            } else
            {
                return new Integer(boaBo.getDirection().getValue());
            }
        } else if (BookOfAccountInputPanel.MONEY.equals(property))
        {
            return boaBo.getMoney().toString();
        } else
        {
            return null;
        }
    }

    /**
     * Modifies the element
     * 
     * @param element the element
     * @param property the property
     * @param value the value
     */
    public void modify(Object element, String property, Object value)
    {
        if (element instanceof Item)
            element = ((Item) element).getData();

        BookOfAccountBo boaBo = (BookOfAccountBo) element;
        if (BookOfAccountInputPanel.SUBJECT_NAME.equals(property))
        {
            AccountSubjectBo subject = (AccountSubjectBo) value;
            boaBo.setAccountSubject(subject);
        } else if (BookOfAccountInputPanel.DIRECTION.equals(property))
        {
            int index = ((Integer) value).intValue();
            boaBo.setDirection(Direction.fromInt(index));
        } else if (BookOfAccountInputPanel.MONEY.equals(property))
        {
            String stringValue = (String) value;
            if (stringValue != null && stringValue.length() > 0)
            {
                if (!Util.validDouble(stringValue))
                {
                    MessageBox messageBox = new MessageBox(
                        shell,
                        SWT.ICON_INFORMATION | SWT.OK);
                    messageBox
                        .setText(Messages
                            .getString("BookOfAccountInputCellModifier.prompt.info")); //$NON-NLS-1$
                    messageBox
                        .setMessage(Messages
                            .getString("BookOfAccountInputCellModifier.input.not.correct")); //$NON-NLS-1$
                    messageBox.open();
                    return;
                }
                boaBo.setMoney(Util.toDouble(stringValue));
            }
        }

        // Force the viewer to refresh
        viewer.refresh();
    }
}